/**
 * Storage二次封装
 * @time   2022-9-1 17:57:19
 * @author Bliss
 */

const MyStore = (function() {
  const _myStore = function(namespace = '') {
    if (!(this instanceof _myStore)) {
      return new _myStore(namespace)
    }

    this.namespace = namespace
  }

  // 构建原型
  _myStore.prototype = {
    // 查询指定
    setItem: function(key, val) {
      const storage = this.getStorage()
      storage[key] = val
      this.saveStorage(storage)
    },

    // 查询指定
    getItem: function(key) {
      return this.getStorage()[key] || false
    },

    // 删除指定
    removeItem: function(key) {
      const storage = this.getStorage()
      delete storage[key]
      this.saveStorage(storage)
    },

    // 删除全部
    clearAll: function() {
      localStorage.removeItem(this.namespace)
    },

    // 查询全部
    getStorage: function() {
      return JSON.parse(localStorage.getItem(this.namespace) || '{}')
    },

    // 保存数据
    saveStorage: function(data) {
      localStorage.setItem(this.namespace, JSON.stringify(data))
    }
  }

  return _myStore
})()

import config from '@/config'
export default new MyStore(config.namespace || '')
